home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Pascal Super Library
/
Pascal Super Library (CW International)(1997).bin
/
DELPHI32
/
DEBUG
/
OBJTEST
/
OBJTEST.ZIP
/
main.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1996-06-06
|
5KB
|
166 lines
unit Main;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, ExtCtrls, Clkheadb, StdCtrls, objunit;
type
TfrmMain = class(TForm)
Memo1: TMemo;
butAddObject: TButton;
Label2: TLabel;
labMean: TLabel;
butDeleteObject: TButton;
edtString: TEdit;
Label6: TLabel;
labString: TLabel;
Button4: TButton;
panHoldMemoObject: TPanel;
lstObjectString: TListBox;
chkAutoCalc: TCheckBox;
Label1: TLabel;
edtControl: TEdit;
Memo2: TMemo;
Label3: TLabel;
Shape1: TShape;
procedure butAddObjectClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure butDeleteObjectClick(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure lstObjectStringClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Memo1Enter(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure DeleteObject(idx: integer);
procedure OnDataChange(Sender: TObject);
end;
var
frmMain: TfrmMain;
MyStringList: TStringList;
MyObject: TMyObjectClass;
implementation
{$R *.DFM}
{''----------------------------------------------------------------------------}
{3. Everytime the text in the memo changes, this procedure is fired. Mean is a
read only property of TMyObjectClass. Everytime its value is retrived, the
function GetMean is executed.}
procedure TfrmMain.OnDataChange(Sender: TObject);
begin
if chkAutoCalc.Checked then begin
with TMyObjectClass(MyStringList.Objects[lstObjectString.ItemIndex]) do begin
labMean.Caption := 'Mean = ' + FloatToStr(Mean);
end;
end;
end;
{''----------------------------------------------------------------------------}
{A new object is created and associated with a new string in MyStringList}
procedure TfrmMain.butAddObjectClick(Sender: TObject);
begin
{Create the object and assign ownership to TfrmMain}
MyObject := TMyObjectClass.Create(Self);
{FMemo itself is created in the MyObject constructor. But since TMemo is
decended from a TWinControl, it requires a parent. In this can we are putting
it into a TPanel}
MyObject.FMemo.Parent := panHoldMemoObject;
{Set the align property and copy the text from memo1}
MyObject.FMemo.Align := alClient;
MyObject.FMemo.Lines := memo1.lines;
{The AddObject method allows adding a string and associated object at the
same time}
MyStringList.AddObject(edtString.text, MyObject);
{This listbox allows us to select which object is displayed in the panel
panHoldMemoObject}
lstObjectString.Items.Add(edtString.text);
lstObjectString.ItemIndex := lstObjectString.Items.Count - 1;
labString.Caption := edtString.text;
{When the objects event OnDataChange is fired execute the code in the TfrmMain
method OnDataChange}
MyObject.OnDataChange := OnDataChange;
{Make sure the initial calculation is performed}
OnDataChange(Self);
end;
{''----------------------------------------------------------------------------}
procedure TfrmMain.FormCreate(Sender: TObject);
begin
MyStringList := TStringList.Create;
end;
{''----------------------------------------------------------------------------}
procedure TfrmMain.butDeleteObjectClick(Sender: TObject);
begin
if lstObjectString.ItemIndex > -1 then begin
DeleteObject(lstObjectString.ItemIndex);
end else begin
showmessage('You need to select an item from the listbox below.');
end;
end;
{''----------------------------------------------------------------------------}
procedure TfrmMain.Button4Click(Sender: TObject);
begin
with TMyObjectClass(MyStringList.Objects[lstObjectString.ItemIndex]) do begin
edtControl.Text := FloatToStr(CalcMean(TStringList(memo1.lines)));
end;
end;
{''----------------------------------------------------------------------------}
procedure TfrmMain.lstObjectStringClick(Sender: TObject);
begin
labString.Caption := MyStringList[lstObjectString.ItemIndex];
with TMyObjectClass(MyStringList.Objects[lstObjectString.ItemIndex]) do begin
FMemo.BringToFront;
labMean.Caption := 'Mean = ' + FloatToStr(Mean);
end;
end;
{''----------------------------------------------------------------------------}
procedure TfrmMain.FormDestroy(Sender: TObject);
var
i: integer;
begin
for i := MyStringList.Count - 1 downto 0 do begin
DeleteObject(i);
end;
MyStringList.Free;
end;
{''----------------------------------------------------------------------------}
procedure TfrmMain.DeleteObject(idx: integer);
begin
MyStringList.Objects[idx] := nil;
TMyObjectClass(MyStringList.Objects[idx]).Free;
MyStringList.Delete(idx);
lstObjectString.Items.Delete(idx);
end;
procedure TfrmMain.Memo1Enter(Sender: TObject);
begin
(Sender as TMemo).Clear;
end;
end.